/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package commons.httpclient;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.utils.URIUtils;
import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicHttpResponse;
import org.apache.http.message.BasicNameValuePair;

/**
 *
 * @author WangShuai
 */
public class Demo2 {

    public static void main(String[] args) throws Exception {
        //1.生成URI地址
        List<NameValuePair> qparams = new ArrayList<NameValuePair>();
        qparams.add(new BasicNameValuePair("q", "httpclient"));
        qparams.add(new BasicNameValuePair("btnG", "Google Search"));
        qparams.add(new BasicNameValuePair("aq", "f"));
        qparams.add(new BasicNameValuePair("oq", null));
        URI uri = URIUtils.createURI("http", "www.google.com", -1, "/search",
                URLEncodedUtils.format(qparams, "UTF-8"), null);
        HttpGet httpget = new HttpGet(uri);
        System.out.println(httpget.getURI()); //http://www.google.com/search?q=httpclient&btnG=Google+Search&aq=f&oq=

        //2.HttpResponse 
        HttpResponse response = new BasicHttpResponse(HttpVersion.HTTP_1_1,
        HttpStatus.SC_OK, "OK");
        System.out.println(response.getProtocolVersion());  //HTTP/1.1
        System.out.println(response.getStatusLine().getStatusCode());  //200
        System.out.println(response.getStatusLine().getReasonPhrase());  //OK
        System.out.println(response.getStatusLine().toString());  //HTTP/1.1 200 OK
    }
}
